/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.client.view;

import java.util.ArrayList;

import org.easypr.organizer.client.presenter.CreatePresenter;
import org.easypr.organizer.shared.MyContact;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

// TODO: Auto-generated Javadoc
/**
 * The Class MenuView.
 */
public class CreateView extends Composite implements CreatePresenter.Display {

	/** The container. */
	FlowPanel container;
	
	/** The table. */
	FlexTable table;
	
	/** The name input. */
	TextBox nameInput;
	
	/** The email input. */
	TextBox emailInput;
	
	/** The cellphone input. */
	TextBox cellphoneInput;
	
	/** The workphone input. */
	TextBox workphoneInput;
	
	/** The workplace input. */
	TextBox workplaceInput;
	
	/** The background input. */
	TextArea backgroundInput;
	
	/** The save button. */
	Button saveButton;
	
	/** The cancel button. */
	Button cancelButton;
	
	/** The history. */
	ArrayList<MyContact> history;
	

	/**
	 * Instantiates a new menu view.
	 */
	public CreateView() {
		container = new FlowPanel();
		initWidget(container);
		table = new FlexTable();
		nameInput = new TextBox();
		emailInput = new TextBox();
		cellphoneInput = new TextBox();
		workphoneInput = new TextBox();
		workplaceInput = new TextBox();
		backgroundInput = new TextArea();
		saveButton = new Button("Save");
		cancelButton = new Button("Cancel");
		history = new ArrayList<MyContact>();
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#addToHistory(org.easypr.organizer.shared.MyContact)
	 */
	public void addToHistory(MyContact contact) {
		this.history.add(contact);
		//this.history.add(this.history.size(), contact);
		if(this.history.size() == 6) {
			this.history.remove(0);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#updateHistory()
	 */
	public void updateHistory() {
		// Render the history list
		table.clearCell(0, 1);
		FlowPanel historyPanel = new FlowPanel();
		historyPanel.addStyleName("history-panel");
		
		Label heading = new Label("History");
		heading.addStyleName("heading");
		historyPanel.add(heading);

		for(int i = this.history.size()-1; i>=0; i--) {
			
			FlowPanel item = new FlowPanel();
			item.setStyleName("contact-item");
			
			Label name = new Label(history.get(i).getName());
			name.addStyleName("contact-name");
			name.addStyleName("bold");
			item.add(name);
			if(history.get(i).getEmail().length() > 0) {
				item.add(new Label(history.get(i).getEmail()));
			}
			if(history.get(i).getCellphone().length() > 0) {
				item.add(new Label("Cell: " + history.get(i).getCellphone()));
			}
			if(history.get(i).getWorkphone().length() > 0) {
				item.add(new Label("Workphone: " + history.get(i).getWorkphone()));
			}
			if(history.get(i).getWorkplace().length() > 0) {
				item.add(new Label("Workplace: " + history.get(i).getWorkplace()));
			}
			
			// TODO: Add edit and delete contact links
			
			historyPanel.add(item);
		}
		table.setWidget(0, 1, historyPanel);
		
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#clearForm()
	 */
	public void clearForm() {
		nameInput.setText("Name*");
		emailInput.setText("Email");
		cellphoneInput.setText("Cellphone");
		workphoneInput.setText("Work phone");
		workplaceInput.setText("Work place");
		backgroundInput.setText("");
		saveButton.setEnabled(true);
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#getCancelButton()
	 */
	public Button getCancelButton() {
		return this.cancelButton;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#getNameInput()
	 */
	public TextBox getNameInput() {
		return this.nameInput;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#getEmailInput()
	 */
	public TextBox getEmailInput() {
		return this.emailInput;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#getCellphoneInput()
	 */
	public TextBox getCellphoneInput() {
		return this.cellphoneInput;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#getWorkphoneInput()
	 */
	public TextBox getWorkphoneInput() {
		return this.workphoneInput;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#getWorkplaceInput()
	 */
	public TextBox getWorkplaceInput() {
		return this.workplaceInput;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#getBackgroundInput()
	 */
	public TextArea getBackgroundInput() {
		return this.backgroundInput;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#getSaveButton()
	 */
	public Button getSaveButton() {
		return this.saveButton;
	}
	
	

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#setData()
	 */
	public void setData() {
		container.clear();
		
		
		HorizontalPanel header = new HorizontalPanel();
		header.setStyleName("content-header");
		
		Label lbl = new Label("Create new contact");
		lbl.setStyleName("header-label");
		
		header.add(lbl);
		container.add(header);
		
		
		table.setWidget(0, 0, getAddWidget());
		table.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		table.setText(0, 1, "");
		table.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
		
		container.add(table);
		
	}

	/**
	 * Gets the adds the widget.
	 *
	 * @return the adds the widget
	 */
	public FlowPanel getAddWidget() {
		FlowPanel body = new FlowPanel();
		body.setStyleName("content-body");
		
		Label heading = new Label("Fill in the contact information");
		heading.addStyleName("heading");
		container.add(heading);
		
		FlexTable table = new FlexTable();
		clearForm();
		
		table.getFlexCellFormatter().setColSpan(0, 0, 2);
		nameInput.setWidth("100%");
		nameInput.addStyleName("bold");
		table.setWidget(0, 0, nameInput);

		table.getFlexCellFormatter().setColSpan(1, 0, 2);
		
		emailInput.setWidth("100%");
		table.setWidget(1, 0, emailInput);
		
		table.setWidget(2, 0, cellphoneInput);
		
		workphoneInput.setWidth("100%");
		table.setWidget(2, 1, workphoneInput);
		
		table.getFlexCellFormatter().setColSpan(3, 0, 2);
		workplaceInput.setWidth("100%");
		table.setWidget(3, 0, workplaceInput);
		
		table.getFlexCellFormatter().setColSpan(4, 0, 2);
		table.setText(4, 0, "How do you know this person?");
		
		table.getFlexCellFormatter().setColSpan(5, 0, 2);
		table.setWidget(5, 0, backgroundInput);
		
		saveButton.addStyleName("save-button");
		HorizontalPanel buttons = new HorizontalPanel();
		buttons.add(saveButton);
		buttons.add(cancelButton);
		
		buttons.setCellWidth(saveButton, "110px");
		table.setWidget(6, 0, buttons);
		
		
		body.add(table);
		
		return body;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.CreatePresenter.Display#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return this;
	}
}
